//
//  JSSeetingUserIconVC.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/22.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSSeetingUserIconVC.h"
#import <TZImageManager.h>
@interface JSSeetingUserIconVC ()<TZImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UIImageView *useriConImagev;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnheight;
@property (strong, nonatomic) UIImage *selectImage;
@property (nonatomic, strong) JSTipsView *progressView;
@end

@implementation JSSeetingUserIconVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor = JS_colorWithHex(kBodyBGColor);
    [self.confirmBtn setTitle:JSLocalizedString(kLocalizableOk) forState:UIControlStateNormal];
    self.title = JSLocalizedString(@"userIcon");
    self.btnheight.constant = kRealValue(44);
}
- (IBAction)selectImageClick:(id)sender {
    TZImagePickerController *pickerController = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    pickerController.allowPickingVideo = NO;
    pickerController.allowCrop = YES;
    pickerController.cropRect = CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH)*0.5, SCREEN_WIDTH, SCREEN_WIDTH);
    pickerController.cropRectPortrait = pickerController.cropRect;
//    pickerController.needCircleCrop   = YES;
    [self.navigationController presentViewController:pickerController animated:YES completion:nil];
}
- (IBAction)confirmClick:(id)sender {
    // 图片上传
    if (!self.selectImage) {
        [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"setingusericon") inView:self.view animation:YES];
        return;
        
    }
    JS_WeakSelf(self)
    [self.view addSubview:self.progressView];
    [self.progressView showWithProgress:0];
    [UserManager uploadImage:@[self.selectImage] progress:^(CGFloat p) {
        if (p >= 1.0) {
            UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            [indicator startAnimating];
            weakself.progressView.changeToCustomView = indicator;
            weakself.progressView.contentCustomText = @"设置中..";
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakself.progressView resetProgressValue:p isAnimation:YES isFullHiden:YES];
        });
    } respons:^(id r) {
        weakself.parmas[@"src"] = [NSString stringWithFormat:@"%@%@", BASE_URL, r];
        [JSNetManager js_courrentProjectRequestWithMouth:@"setAvatar" cache:NO ifShowFinishStatu:NO parameters:weakself.parmas successBlock:^(id response) {
            if ([response[@"status"] integerValue] == 200) {
                [weakself.navigationController popToRootViewControllerAnimated:YES];
                [JSAlertHelper showSucceedHideAfterDelay:2 text:NSStringFormat(@"%@%@",JSLocalizedString(kLocalizableSeeting),JSLocalizedString(kLocalizableSuccess)) inView:nil animation:YES];
            }
        } failureBlock:nil];
    } failur:^(NSError *e) {
        [weakself.progressView hideAnimated:YES];
    }];
    
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
    UIImage *image = photos.firstObject;
    self.selectImage = image;
    self.useriConImagev.image = [[image imageByResizeToSize:CGSizeMake(200, 200)] imageByRoundCornerRadius:100];
    if (!isSelectOriginalPhoto) {
        [[TZImageManager manager] getOriginalPhotoWithAsset:assets.firstObject completion:^(UIImage *photo, NSDictionary *info) {
            JSLog(@"%@", NSStringFromCGSize(photo.size));
        }];
    }
}
-(JSTipsView *)progressView{
    if (!_progressView) {
        _progressView = [[JSTipsView alloc] initWithView:self.view];
        _progressView.removeFromSuperViewWhenHide = YES;
        [_progressView resetProgressTintColor:[UIColor whiteColor]];
    }
    return _progressView;
}
@end
